import store from '@/store/index.js'
export const isCollection = (pId) => {

	if (!store.state.vuex_user) {
		return false;
	}

	let collectionIds = uni.getStorageSync("collectionIds")
	if (!collectionIds || collectionIds.length < 1) {
		return false;
	}

	if (collectionIds.findIndex((id) => id === pId) < 0) {
		return false;
	}

	return true;
}



export const collectionHandler = (pId, status) => {
	if (!store.state.vuex_user) {
		return false;
	}

	let collectionIds = uni.getStorageSync("collectionIds")
	// 取消收藏
	if (status == 0) {
		if (!collectionIds || collectionIds.length < 1) {
			return false;
		}
		let index = collectionIds.findIndex((id) => id === pId)
		if (index < 0) {
			return false;
		}
		collectionIds.splice(index, 1)
		uni.setStorage({
			key: "collectionIds",
			data: collectionIds
		})
	} else {

		if (!collectionIds) {
			collectionIds = [pId]
		} else {
			collectionIds.push(pId);
		}

		uni.setStorage({
			key: "collectionIds",
			data: collectionIds
		})
	}

	return true;
}
